/**
 *  julive
 *
 *  Create by songli on 2021/07/07
 *  Copyright © 2021 居理买房网. All rights reserved.
 */

package client

var shorturls map[string]*ShorturlClient

var opts = Options{"shorturl"}

func init() {
        shorturls = make(map[string]*ShorturlClient)
}

type Options struct {
        Name string
}

func New(opt ...Option) (*ShorturlClient, error) {
        for _, o := range opt {
                o(&opts)
        }
        shorturlClient, ok := shorturls[opts.Name]
        if !ok {
                shorturlClient, err := NewShorturlClient(opts.Name)
                if err == nil {
                        shorturls[opts.Name] = shorturlClient
                }
                return shorturlClient, err
        }
        return shorturlClient, nil
}

type Option func(*Options)

func Name(name string) Option {
        return func(o *Options) {
                o.Name = name
        }
}

func Close() {
        for _, client := range shorturls {
                client.Close()
        }
}
